package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.login.LoginUser;
import com.atguigu.lease.common.login.LoginUserHolder;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.LeaseAgreementService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.service.UserInfoService;
import com.atguigu.lease.web.app.vo.agreement.AgreementDetailVo;
import com.atguigu.lease.web.app.vo.agreement.AgreementItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Override
    public List<AgreementItemVo> listItem() {

        return leaseAgreementMapper.listAgreementItemByPhone(LoginUserHolder.getLoginUser().getUsername());
    }

    @Override
    public AgreementDetailVo getDetailByAgreementId(Long id) {
        // 1.查询租约信息
        LeaseAgreement leaseAgreement = getById(id);
        if (leaseAgreement == null) {
            return null;
        }
        // 2.查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectApartmentById(leaseAgreement.getApartmentId());
        // 3.查询房间信息
        RoomInfo roomInfo = roomInfoMapper.selectRoomById(leaseAgreement.getRoomId());
        // 4.查询公寓图片
        List<GraphVo> apartmentGraphVoList = graphInfoMapper.selectListByItemAndApartmentId(ItemType.APARTMENT, leaseAgreement.getApartmentId());
        // 5.查看房间图片
        List<GraphVo> roomGraphVoList = graphInfoMapper.selectListByItemAndApartmentId(ItemType.ROOM, leaseAgreement.getRoomId());

        // 6.查询租期信息
        LeaseTerm leaseTerm = leaseTermMapper.selectByLeaseTermId(leaseAgreement.getLeaseTermId());

        // 7.查询支付方式
        PaymentType paymentType = paymentTypeMapper.selectByPaymentTypeId(leaseAgreement.getPaymentTypeId());


        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();
        BeanUtils.copyProperties(leaseAgreement, agreementDetailVo);
        agreementDetailVo.setApartmentGraphVoList(apartmentGraphVoList);
        agreementDetailVo.setApartmentName(apartmentInfo.getName());
        agreementDetailVo.setRoomGraphVoList(roomGraphVoList);
        agreementDetailVo.setRoomNumber(roomInfo.getRoomNumber());
        agreementDetailVo.setLeaseTermMonthCount(leaseTerm.getMonthCount());
        agreementDetailVo.setLeaseTermUnit(leaseTerm.getUnit());
        agreementDetailVo.setPaymentTypeName(paymentType.getName());
        return agreementDetailVo;
    }

    @Override
    public void updateStatusByAgreementId(Long id, LeaseStatus leaseStatus) {
        LambdaUpdateWrapper<LeaseAgreement> leaseAgreementLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        leaseAgreementLambdaUpdateWrapper
                .eq(LeaseAgreement::getId, id)
                .set(LeaseAgreement::getStatus, leaseStatus.getCode());
        this.update(leaseAgreementLambdaUpdateWrapper);
    }

    @Override
    public List<LeaseTerm> listByRoomId(Long roomId) {
        return leaseAgreementMapper.listByRoomId(roomId);
    }
}





